import { list, summary } from '../../../../api/point'
const util = require('../../../../utils/util.js')

const app = getApp()
Page({

  /**
   * 页面的初始数据
   */
  data: {
    pageNum: 1,
    pageSize: 20,
    detailedList: [],
    today: '',
    month: "",
    Month: '',
    bgColor: 'rgba(0,0,0,0.9)',
    credit: '',//收入
    debit: '', //支出
    type: '',
    typeName: "全部明细",
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    var date = new Date()
    this.data.today = date
    this.data.month = this.formatData("month", date)
    this.data.Month = util.datetime(date).substr(0,7)
    this.setData({
      today: this.data.today,
      month: this.data.month,
      Month: this.data.Month,
      type:''
    })
  },

  /**z
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    this.data.pageNum = 1,
    this.data.pageSize = 20,
    this.loadList()
    this.loadSummary()
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  onPullDownRefresh() {
    wx.showNavigationBarLoading()
    this.data.pageNum = 1
    setTimeout(() => {
      wx.hideNavigationBarLoading()
      this.loadList()
      wx.stopPullDownRefresh()
    }, 1000);
  },

  onReachBottom() {
    this.data.pageNum += 1
    this.loadList()
  },
  loadList() {
    if (this.data.pageNum == 1) {
      this.data.detailedList = []
    }
    list({
      type:this.data.type,
      month: this.data.Month,
      pageSize: this.data.pageSize,
      pageNum: this.data.pageNum
    }).then(res => {
      res.data.data.forEach(item => {
        item.createDate = util.datetimeFormat(item.createDate)
        item.thumbnail = 'https://cdn.rzico.com/v4.0/wallet-consumption.png'
        let credit = item.credit / 100;
        item.credit = credit.toFixed(2)
        let debit = item.debit / 100;
        item.debit = debit.toFixed(2)
        let balance = item.balance / 100
        item.balance = balance.toFixed(2)
        this.data.detailedList.push(item)
      })
      this.setData({
        detailedList: this.data.detailedList
      })
    })
  },
  loadSummary() {
    summary({
      type: this.data.type,
      month: this.data.Month,
      pageSize: this.data.pageSize,
      pageNum: this.data.pageNum
    }).then(res => {
      if (res.data[0] != null) {
        res.data.forEach(item => {
          let credit = item.credit / 100;
          item.credit = credit.toFixed(2)
          this.data.credit = item.credit
          let debit = item.debit / 100;
          item.debit = debit.toFixed(2)
          this.data.debit = item.debit
        })
      }else{
        this.data.credit = 0.00
        this.data.debit = 0.00
      }
      this.setData({
        credit: this.data.credit,
        debit: this.data.debit,
      })
    })
  },


  //选择日期
  bindDateChange(e) {
    let date = e.detail.value;
    // this.data.choseData = e.detail.value;
    this.data.month = this.formatData("month", date)
    this.data.Month = date.substr(0,7)
    this.setData({
      month: this.data.month,
      Month: this.data.Month,
    })
    this.loadSummary()
    this.loadList()
  },
  //格式化时间 
  formatData: function (type, date) {
    var date = new Date(date);
    var year = date.getFullYear()
    var month = date.getMonth() + 1
    var day = date.getDate()
    var D = year + "年" + month + '月' + day + "日"
    var M = year + "年" + month + '月'
    var Y = year + "年"
    // [year, month, day].map(formatNumber).join('-')
    switch (type) {
      case "day": return D;
        break;
      case "month": return M;
        break;
      case "year": return Y;
        break;
      default: [year, month].map(formatNumber).join('-')
    }
  },


  //打开筛选框
  toOppen() {
    this.selectComponent('#divPopup').showFrame();
  },

  selectedInfo(e) {
    let _this = this;
    if(!util.isNull(e.detail.moreType) && e.detail.moreType.id>0){
      this.data.type = e.detail.moreType.id
      this.data.typeName = e.detail.moreType.name
    }else if(e.detail.moreType.id==0 || util.isNull(e.detail.moreType)){
      if(!util.isNull(e.detail.type)||e.detail.type>0){
        this.data.type = e.detail.type.id
        this.data.typeName = e.detail.type.name
      }else{
        this.data.type = e.detail.moreType.id
        this.data.typeName = e.detail.moreType.id==0?e.detail.moreType.name+"明细":e.detail.moreType.name
      }
    }
    if(util.isNull(e.detail.moreType) && util.isNull(e.detail.type)){
      this.data.type = ''
    }
    this.data.type = this.data.type == 0?'':this.data.type
    _this.setData({
      type: _this.data.type,
      typeName: _this.data.typeName
    })
    this.loadSummary()
    this.loadList()
  },
  toStatistics() {
    wx.navigateTo({
      url: '/pages/wallet/jumpPage/distribution/statistics',
    })
  },

})
